package cn.kgc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.kgc.entity.ResumeWorkExp;
import cn.kgc.form.ResumeWorkExpUpdateForm;
import cn.kgc.mapper.ResumeWorkExpMapper;
import cn.kgc.service.ResumeWorkExpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service("resumeWorkExpService")
@SuppressWarnings("all")
public class ResumeWorkExpServiceImpl extends ServiceImpl<ResumeWorkExpMapper, ResumeWorkExp> implements ResumeWorkExpService {

    @Override
    public boolean delete(String id, String userId, String resumeId) {
        LambdaQueryWrapper<ResumeWorkExp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResumeWorkExp::getId, id);
        queryWrapper.eq(ResumeWorkExp::getUserId, userId);
        queryWrapper.eq(ResumeWorkExp::getResumeId, resumeId);
        return remove(queryWrapper);
    }

    @Override
    public boolean addOrUpdate(ResumeWorkExpUpdateForm form,String userId) {
         if(StrUtil.isBlank(form.getId())){
             //新增操作
             ResumeWorkExp resumeWorkExp = new ResumeWorkExp();
             BeanUtil.copyProperties(form,resumeWorkExp);
             resumeWorkExp.setUserId(userId);
             return save(resumeWorkExp);
         }else{
             //修改操作
             LambdaUpdateWrapper<ResumeWorkExp> updateWrapper = new LambdaUpdateWrapper<>();
             //设置值
             updateWrapper.set(ResumeWorkExp::getCompanyName, form.getCompanyName());
             updateWrapper.set(ResumeWorkExp::getIndustry, form.getIndustry());
             updateWrapper.set(ResumeWorkExp::getBeginDate, form.getBeginDate());
             updateWrapper.set(ResumeWorkExp::getEndDate, form.getEndDate());
             updateWrapper.set(ResumeWorkExp::getPosition,form.getPosition());
             updateWrapper.set(ResumeWorkExp::getDepartment, form.getDepartment());
             updateWrapper.set(ResumeWorkExp::getContent, form.getContent());
             updateWrapper.eq(ResumeWorkExp::getContentHtml, form.getContentHtml());
             //条件
             updateWrapper.eq(ResumeWorkExp::getId, form.getId());
             updateWrapper.eq(ResumeWorkExp::getUserId, userId);
             updateWrapper.eq(ResumeWorkExp::getResumeId, form.getResumeId());
             //更新数据
             return update(updateWrapper);
         }
    }

    @Override
    public List<ResumeWorkExp> queryByResumeId(String resumeId, String userId) {
        LambdaQueryWrapper<ResumeWorkExp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResumeWorkExp::getResumeId, resumeId);
        queryWrapper.eq(ResumeWorkExp::getUserId, userId);
        queryWrapper.orderByDesc(ResumeWorkExp::getBeginDate);
        return list(queryWrapper);
    }

}